<?php
/**
 * Created by PhpStorm.
 * User: Adminstratot
 * Date: 2018/11/13
 * Time: 20:55
 */
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends Controller{
    protected $artModel;

    public function __construct(){
        parent::__construct();
        $this->artModel=D('article');
        $this->verification();
    }

    //文章列表模板
    public function article(){
        if(isset($_GET['cid'])) {
            $this->queryfile();
        }else{
            $this->artlist();
        }
        $this->filecate(1);
        $this->albumlist();
        $this->artcate(2);
        $this->userlist();
        $this->display();
    }

    public function content(){
        $art_all=$this->artModel->select();
        $aid=I('get.aid');
        //var_dump($aid);
        foreach($art_all as $art){
            if($art['aid']==$aid){
                $this->assign('title',$art['title']);
                $this->assign('content',$art['content']);
            }
        }
        $this->display();
    }

    //文章回收站模板
    public function articlebin(){
        $this->filecate(1);
        $this->artcate(2);
        $this->albumlist();
        $this->userlist();
        $this->binlist();
        $this->display();
    }

    //验证是否登陆
    protected function verification(){
        $userModel=D('user');
        $userinfo=$userModel->where(array('account'=>cookie('account')))->find();
        if(!che() || !$userinfo){
            $url=U('User/login');
            echo "<script>alert('对不起，请您先登陆。');location.href='$url';</script>";
        }
    }

    //获取文件栏目
    protected function filecate($pid){
        $cateModel=D('cate');
        $filecate=$cateModel->gettree($pid);
        $this->assign('filecate',$filecate);
    }

    //获取文章栏目
    protected function artcate($pid){
        $cateModel=D('cate');
        $artcate=$cateModel->gettree($pid);
        $this->assign('artcate',$artcate);
    }

    //获取搜索栏的相册栏目
    protected function albumlist(){
        $albumModel=D('album');
        $albumlist=$albumModel->select();
        $this->assign('albumlist',$albumlist);
    }

    //获取用户列表
    protected function userlist(){
        $userModel=D('userinfo');
        $userlist=$userModel->select();
        $this->assign('userlist',$userlist);
    }

    //第一页与最后一页的提示
    protected function page_tips(){
        $art_all=$this->artModel->where('is_del=0')->select();
        $art_count = count($art_all);
        if(I('get.start')<0){
            echo "<script>alert('已经第一页了');window.location.href=document.referrer;</script>";
        }else if(I('get.start')>$art_count){
            echo "<script>alert('已经是最后一页了');location.href='javascript:history.go(-1)';</script>";
        }
    }

    //文章列表
    protected function artlist(){
        $this->page_tips();
        $start=isset($_GET['start'])?$_GET['start']+0:0;
        $pagecount = 10;
        $art_list=$this->artModel->where('is_del=0')->order('aid desc')->limit($start . ',' . $pagecount)->select();
        $art_all=$this->artModel->where('is_del=0')->select();
        if($art_list === false || $art_all === false) {
            $this->error("获取文章列表失败");
        }else {
            //$art_rowcount = count($art_list);
            $art_count = count($art_all);
        }
        //$this->assign('art_rowcount',$art_rowcount);
        $this->assign('art_count',$art_count);
        $this->assign('pagecount',$pagecount);
        $this->assign('art_list',$art_list);
        //$this->assign('art_all',$art_all);
    }

    //文章栏目选择查询
    protected function queryfile(){
        $this->page_tips();
        $start=isset($_GET['start'])?$_GET['start']+0:0;
        $pagecount = 10;
        $query_file=$this->artModel->where('is_del=0 and cid=' . I('cid'))->order('aid desc')->limit($start . ',' . $pagecount)->select();
        $query_all=$this->artModel->where('is_del=0 and cid=' . I('cid'))->select();
        if($query_file === false || $query_all === false) {
            $this->error("获取栏目文章列表失败");
        }else {
            //$query_rowcount = count($query_file);
            $query_count = count($query_all);
        }
        //$this->assign('file_rowcount',$query_rowcount);
        $this->assign('art_count',$query_count);
        $this->assign('pagecount',$pagecount);
        $this->assign('art_list',$query_file);
        //$this->assign('file_all',$query_all);
    }

    //文章回收站列表
    protected function binlist(){
        $this->page_tips();
        $start=isset($_GET['start'])?$_GET['start']+0:0;
        $pagecount = 10;
        $bin_list=$this->artModel->where('is_del=1')->order('aid desc')->limit($start . ',' . $pagecount)->select();
        $bin_all=$this->artModel->where('is_del=1')->select();
        if($bin_list === false || $bin_all === false) {
            $this->error("获取文章回收站列表失败");
        }else {
            //$bin_rowcount = count($bin_list);
            //echo "查询到的结果行数：" . $rowcount . "<br/>";
            $bin_count = count($bin_all);
            //echo "总行数：$count<br/>";
        }
        //$this->assign('bin_rowcount',$bin_rowcount);
        $this->assign('bin_count',$bin_count);
        $this->assign('pagecount',$pagecount);
        $this->assign('bin_list',$bin_list);
        //$this->assign('bin_all',$bin_all);
    }

    /*
    //文章下载
    public function download(){
        if(I('get.fid')){
            $con['fid']=I('get.fid');
            $fileModel=D('File');
            $file_data=$fileModel->where($con)->select();
            $root_path=$_SERVER['DOCUMENT_ROOT'];
            $file_path=$file_data[0]['file'];
            $file_name=$file_data[0]['name'];
            $file=$root_path . '/Public/' . $file_path;
            import('ORG.Net.Http');
            $http=new \Org\Net\Http;
            $fileModel->where($con)->setInc('down_num');
            $http->download($file,$file_name);
        }
    }
    */

    //将文章放入回收站
    public function artdel(){
        if(I('get.aid')){
            $data['is_del']=1;
            $con['aid']=I('get.aid');
            if($this->artModel->where($con)->save($data)){
                echo "<script>alert('成功放入回收站!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
            }
        }
    }

    //将文章从回收站中还原
    public function binres(){
        if(I('get.aid')){
            $data['is_del']=0;
            $con['aid']=I('get.aid');
            if($this->artModel->where($con)->save($data)){
                echo "<script>alert('成功还原文件!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
            }
        }
    }

    //将文章从回收站中删除
    public function bindel(){
        if(I('get.aid')){
            if($this->artModel->delete(I('get.aid'))){
                echo "<script>alert('删除成功!');location.href=document.referrer;</script>";
            }
        }
    }

}
?>